import axios from "axios";

// const instance = axios.create({
//     baseURL: 'http://192.168.227.166:80',
//     timeout: 1000, //ajax请求超时的时间设置
//     headers: {'X-Custom-Header': 'foobar'} //设置请求头
// });

axios.defaults.baseURL = "http://81.68.170.93:8065"; //配置了axios
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.timeout=1000;
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
  function (config) {
    //config就是请求的配置
    // 在发送请求之前做一些事情
    let Authorization = sessionStorage.getItem("Authorization"); //从缓存里面取token
    Authorization && (config.headers.Authorization = Authorization); //如果有token就在请求头里设置token
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
axios.interceptors.response.use(
  function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  },
  function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    switch (error.response.status) {
      case 401: //token有问题
        sessionStorage.clear();
        alert("重新登陆");
        window.location.href = "/login";
        break;
      case 10086: //根据业务，在这里配置
        break;
    }
    return Promise.reject(error);
  }
);

export default axios;
